package com.brayan.webapp.controller.admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.ConversationScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import com.brayan.webapp.model.general.i18n.Text;
import com.brayan.webapp.model.general.i18n.Translation;
import com.brayan.webapp.repository.general.i18n.TextRepository;

/**
 * Creates Text and translations. For single Text units and multiple text units.
 * 
 * @author Brayan
 * 
 */
@Named
@ConversationScoped
public class TextController implements Serializable {

	private static final long serialVersionUID = 3202395030826155537L;

	@Inject
	private TextRepository textRepoService;

	// /CONTROL DATA///
	private boolean renderTextDetail = true;

	// /DATA///
	private Long id;
	private String token;
	private String text;
	private List<Translation> listOfTranslations = new ArrayList<Translation>();
	private String searchFilter;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public List<Translation> getListOfTranslations() {
		Long id = getId();
		if(id != null)
			listOfTranslations = textRepoService.getTranslationsByTextId(id);
		return listOfTranslations;
	}

	public void setListOfTranslations(List<Translation> listOfTranslations) {
		this.listOfTranslations = listOfTranslations;
	}

	public String getSearchFilter() {
		return searchFilter;
	}

	public void setSearchFilter(String searchFilter) {
		this.searchFilter = searchFilter;
	}

	public List<Text> findTextUnitsByString() {
		if (getSearchFilter() == null || getSearchFilter().isEmpty())
			return textRepoService.getTextsUnits(null);
		else
			return textRepoService.getTextUnitsByTokenOrText(getSearchFilter());
	}

	public List<Text> findTextUnits(Integer nRecords) {
		return textRepoService.getTextsUnits(nRecords);
	}

	public boolean isRenderTextDetail() {
		return renderTextDetail;
	}

	public void setRenderTextDetail(boolean renderTextDetail) {
		this.renderTextDetail = renderTextDetail;
	}

	public void loadTextDetail() {
		HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
				.getRequest();
		String id = req.getParameter("id");
		Text text = textRepoService.getText(Long.parseLong(id));
		setTextUnit(text);
		setRenderTextDetail(true);
	}
	
	public void deleteText(){
		HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
				.getRequest();
		String id = req.getParameter("id");
		textRepoService.deleteText(Long.parseLong(id));
	}

	private void setTextUnit(Text text) {
		setId(text.getId());
		setToken(text.getToken());
		setText(text.getDefaultText());
	}

	/**
	 * Retrieves the translations from the database. Requires a text entity set
	 * in the text controller.
	 * 
	 * @return
	 */
	public List<Translation> getTranslations() {
		Long id = getId();
		if(id == null)
			return new ArrayList<Translation>();
		return textRepoService.getTranslationsByTextId(id);
	}
	
	public List<Translation> getTranslationsByTextId(Long id){
		List<Translation> listOfTrans = new ArrayList<Translation>();
		listOfTrans = textRepoService.getTranslationsByTextId(id);
		return listOfTrans;
	}
	
	/**
	 * Use method to save existing and new text units.
	 */
	public void saveText(){
		Text text = new Text();
		text.setId(getId());
		text.setToken(getToken());
		text.setDefaultText(getText());
		textRepoService.saveTextToken(text);
	}

}
